package la.ge.server.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import la.ge.server.GeServerContext;
import la.ge.server.servlets.gs2ge.GetPlainGSResultServlet;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@SuppressWarnings("serial")
public class URLEncodeOutputUIServlet extends HttpServlet {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	/**
	 * Request parameters:
	 * String srcString
	 */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    	GeServerContext geServerContext = (GeServerContext) this.getServletContext().getAttribute( GeServerContext.ATTRIB_NAME );
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

        String srcString = request.getParameter( "srcString" );
        if( srcString == null ) {
        	srcString = "";
        }
        
        String result = URLEncoder.encode(srcString, "utf-8");
       
        InputStream in = this.getClass().getClassLoader().getResourceAsStream( 
        		"la/ge/server/servlets/URLEncodeOutputUIServlet.html" );
        StringWriter stringWriter = new StringWriter();
        IOUtils.copy( in, stringWriter );
        in.close();
        String resp = stringWriter.toString();
        resp = resp.replace("${result}", result);
        response.getWriter().println( resp );
        
    }
	
}
